package beastick.utility;

//Copyright 2008 Tofig Kareemov, estereos@gmail.com
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.

import java.io.IOException;

/**
 * This class is a collection of small and frequently used functions
 */
public class CUtilityMethods
{
 // Public

 public static String ReplaceCharInString(String inString, char toReplace,
   String replaceWith)
 {
  String cleanedString = "";
  for (int i = 0; i < inString.length(); i++)
  {

   if (inString.charAt(i) == toReplace)
   {
    cleanedString += replaceWith;
   } else
   {
    cleanedString += inString.charAt(i);
   }

  }// end for

  return cleanedString;
 }

 protected String Prompt(String sPrompt)
 {
  System.out.print(sPrompt + ": ");
  String sInput = "";
  int iInput = 0;
  try
  {
   System.in.skip(System.in.available());
   for (; (iInput != (int) ('\r')) && (iInput != (int) ('\n'));)
   {
    iInput = System.in.read();
    if ((iInput != (int) ('\r')) && (iInput != (int) ('\n')))
    {
     sInput = sInput + (char) iInput;
    }
   }
  } catch (IOException e)
  {
  }
  return sInput;
 }

 protected String PromptMandatory(String sPrompt)
 {
  String sInput = "";
  for (; !IsValuable(sInput);)
  {
   System.out.print(sPrompt + ": ");
   int iInput = 0;
   try
   {
    System.in.skip(System.in.available());
    for (; (iInput != (int) ('\r')) && (iInput != (int) ('\n'));)
    {
     iInput = System.in.read();
     if ((iInput != (int) ('\r')) && (iInput != (int) ('\n')))
     {
      sInput = sInput + (char) iInput;
     }
    }
   } catch (IOException e)
   {
   }
  }
  return sInput;
 }

 public final static int ROLByte(int iData)
 {
  iData = iData & 0xFF;
  return ((iData << 1) | (iData >>> 7)) & 0xFF;
 }

 public final static int ROLByte(int iData, int iCounter)
 {
  iCounter = iCounter & 7;
  iData = iData & 0xFF;
  return ((iData << iCounter) | (iData >>> (8 - iCounter))) & 0xFF;
 }

 public final static int Limited(int iInput, int iMin, int iMax)
 {
  return (iInput < iMin) ? iMin : (iInput > iMax) ? iMax : iInput;
 }

 public final static int MinLimited(int iInput, int iMin)
 {
  return (iInput < iMin) ? iMin : iInput;
 }

 public final static int MaxLimited(int iInput, int iMax)
 {
  return (iInput > iMax) ? iMax : iInput;
 }

 public final static boolean IsValuable(String sCandidate)
 {
  return ((sCandidate != null) && !(sCandidate.equals("")) && (sCandidate
    .length() > 0));
 }

 public final static boolean IsValuable(Object oCandidate)
 {
  return (oCandidate != null);
 }

 public final static boolean IsInRange(int iCandidate, int iMin, int iMax)
 {
  return ((iCandidate >= iMin) && (iCandidate <= iMax));
 }

 public static boolean areObjectsEqual(Object obj1, Object obj2)
 {
  if (null == obj1)
  {
   return (null == obj2);
  }

  return obj1.equals(obj2);
 }
}